package com.wechat.controller;


import com.auth.common.result.Result;
import com.wechat.service.WechatMenuService;
import exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 菜单 前端控制器
 * </p>
 *
 * @author tang
 * @since 2023-07-29
 */
@Api(tags = "接入微信接口")
@RestController
@RequestMapping("/admin/wechat/menu")
public class WechatMenuController {
    private final WechatMenuService wechatMenuService;

    public WechatMenuController(WechatMenuService wechatMenuService) {
        this.wechatMenuService = wechatMenuService;
    }

    @ApiOperation("获取所有菜单")
    @GetMapping("/findMenuInfo")
    public Result findMenuInfo() {
        return Result.ok(wechatMenuService.findMenuInfo());
    }

    @ApiOperation("同步菜单")
    @GetMapping("/syncMenu")
    public Result createMenu() {
        try {
            Object o = wechatMenuService.syncMenu();
            return Result.ok(o);
        } catch (Exception e) {
            throw new BusinessException(500, e.getLocalizedMessage());
        }
    }

    @ApiOperation("移除菜单")
    @DeleteMapping("/removeMenu")
    public Result removeMenu() {
        try {
            wechatMenuService.removeMenu();
        } catch (WxErrorException e) {
            throw new BusinessException(500,e.getMessage());
        }
        return Result.ok();
    }
}

